﻿using Autofac;
using CommunityToolkit.Mvvm.Input;
using DBContext;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZoneLine.Models.StationConfig;
using ZoneLine.Stations;

namespace ZoneLine.ViewModels
{
    public class StationConfigViewModel : ViewModelBase
    {
        private IModelDAL _dal;

        public StationConfigViewModel(IModelDAL dal)
        {
            _dal = dal;

            Task.Run(() =>
            {
                Op006Config = _dal.GetModel<QOP006StationConfig>(p => true);
                if (Op006Config == null)
                {
                    Op006Config = new QOP006StationConfig();
                    _dal.AddModel(Op006Config);
                }
            });

            Task.Run(() =>
            {
                Op010Config = _dal.GetModel<QOP010StationConfig>(p => true);
                if (Op010Config == null)
                {
                    Op010Config = new QOP010StationConfig();
                    _dal.AddModel(Op010Config);
                }
            });

            Task.Run(() =>
            {
                Op020Config = _dal.GetModel<QOP020StationConfig>(p => true);
                if (Op020Config == null)
                {
                    Op020Config = new QOP020StationConfig();
                    _dal.AddModel(Op020Config);
                }
            });

            Task.Run(() =>
            {
                Op050LConfig = _dal.GetModel<QOP050LStationConfig>(p => true);
                if (Op050LConfig == null)
                {
                    Op050LConfig = new QOP050LStationConfig();
                    _dal.AddModel(Op050LConfig);
                }
            });

            Task.Run(() =>
            {
                Op050RConfig = _dal.GetModel<QOP050RStationConfig>(p => true);
                if (Op050RConfig == null)
                {
                    Op050RConfig = new QOP050RStationConfig();
                    _dal.AddModel(Op050RConfig);
                }
            });

            Task.Run(() =>
            {
                LoadPorts();
                PackageScanConfig = _dal.GetModel<PackageScanBarConfig>(p => true);
                if (PackageScanConfig == null)
                {
                    PackageScanConfig = new PackageScanBarConfig();
                    _dal.AddModel(PackageScanConfig);
                }
            });

            Task.Run(() =>
            {
                PrinterNames = GetLocalPrinter()?.ToList();
            });
        }

        public string[] GetLocalPrinter()
        {
            string[] ListPrinter = new string[PrinterSettings.InstalledPrinters.Count];
            //获取当前打印机
            for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++)
            {
                ListPrinter[i] = (PrinterSettings.InstalledPrinters[i].ToString());
            }
            return ListPrinter;
        }

        private List<string> _printerNames;
        /// <summary>
        /// 打印机列表名称
        /// </summary>
        public List<string> PrinterNames
        {
            get { return _printerNames; }
            set => SetProperty(ref _printerNames, value);
        }


        private QOP006StationConfig _op006Config;

        public QOP006StationConfig Op006Config
        {
            get { return _op006Config; }
            set => SetProperty(ref _op006Config, value);
        }


        private QOP010StationConfig _op010Config;

        public QOP010StationConfig Op010Config
        {
            get { return _op010Config; }
            set => SetProperty(ref _op010Config, value);
        }

        private QOP020StationConfig _op020Config;

        public QOP020StationConfig Op020Config
        {
            get { return _op020Config; }
            set => SetProperty(ref _op020Config, value);
        }

        private QOP050LStationConfig _op050LConfig;

        public QOP050LStationConfig Op050LConfig
        {
            get { return _op050LConfig; }
            set => SetProperty(ref _op050LConfig, value);
        }

        private QOP050RStationConfig _op050RConfig;

        public QOP050RStationConfig Op050RConfig
        {
            get { return _op050RConfig; }
            set => SetProperty(ref _op050RConfig, value);
        }

        private PackageScanBarConfig _packageScanConfig;

        public PackageScanBarConfig PackageScanConfig
        {
            get { return _packageScanConfig; }
            set => SetProperty(ref _packageScanConfig, value);
        }

        private RelayCommand _saveCmd;

        public RelayCommand SaveCmd
        {
            get { return _saveCmd ?? (_saveCmd = new RelayCommand(Save)); }
        }

        private void Save()
        {
            try
            {
                _dal.UpdateModel<QOP006StationConfig>(p => true, p => new QOP006StationConfig
                {
                    ReadStartAddr = Op006Config.ReadStartAddr,
                    AllowWorkAddr = Op006Config.AllowWorkAddr,
                    EndUploadCodeAddr = Op006Config.EndUploadCodeAddr,
                    EndUploadDataAddr = Op006Config.EndUploadDataAddr,
                    Ip = Op006Config.Ip
                });
            }
            catch (Exception ex)
            {

            }

            try
            {
                _dal.UpdateModel<QOP010StationConfig>(p => true, p => new QOP010StationConfig
                {
                    ReadStartAddr = Op010Config.ReadStartAddr,
                    AllowWorkAddr = Op010Config.AllowWorkAddr,
                    EndUploadCodeAddr = Op010Config.EndUploadCodeAddr,
                    EndUploadDataAddr = Op010Config.EndUploadDataAddr,
                    Ip = Op010Config.Ip
                });
            }
            catch (Exception ex)
            {

            }

            try
            {
                _dal.UpdateModel<QOP020StationConfig>(p => true, p => new QOP020StationConfig
                {
                    ReadStartAddr = Op020Config.ReadStartAddr,
                    AllowWorkAddr = Op020Config.AllowWorkAddr,
                    EndUploadCodeAddr = Op020Config.EndUploadCodeAddr,
                    EndUploadDataAddr = Op020Config.EndUploadDataAddr,
                    Ip = Op020Config.Ip,
                    WorkChannelAddr = Op020Config.WorkChannelAddr,
                    ApiUrl = Op020Config.ApiUrl,
                    EnableApi = Op020Config.EnableApi,
                    ReQueryCount = Op020Config.ReQueryCount,
                });
            }
            catch (Exception ex)
            {

            }

            try
            {
                _dal.UpdateModel<QOP050LStationConfig>(p => true, p => new QOP050LStationConfig
                {
                    ReadStartAddr = Op050LConfig.ReadStartAddr,
                    AllowWorkAddr = Op050LConfig.AllowWorkAddr,
                    EndUploadCodeAddr = Op050LConfig.EndUploadCodeAddr,
                    EndUploadDataAddr = Op050LConfig.EndUploadDataAddr,
                    Ip = Op050LConfig.Ip,
                    PrinterDemoName = Op050LConfig.PrinterDemoName,
                    PrinterName = Op050LConfig.PrinterName,
                });
            }
            catch (Exception ex)
            {

            }

            try
            {
                _dal.UpdateModel<QOP050RStationConfig>(p => true, p => new QOP050RStationConfig
                {
                    ReadStartAddr = Op050RConfig.ReadStartAddr,
                    AllowWorkAddr = Op050RConfig.AllowWorkAddr,
                    EndUploadCodeAddr = Op050RConfig.EndUploadCodeAddr,
                    EndUploadDataAddr = Op050RConfig.EndUploadDataAddr,
                    Ip = Op050RConfig.Ip,
                    PrinterDemoName = Op050RConfig.PrinterDemoName,
                    PrinterName = Op050RConfig.PrinterName,
                });
            }
            catch (Exception ex)
            {

            }

            try
            {
                _dal.UpdateModel<PackageScanBarConfig>(p => true, p => new PackageScanBarConfig
                {
                    Port = PackageScanConfig.Port,
                    Printer = PackageScanConfig.Printer,
                    PrintTemplate = PackageScanConfig.PrintTemplate,
                });

            }
            catch (Exception ex)
            {

            }
        }

        private RelayCommand<string> _openFileCmd;

        public RelayCommand<string> OpenFileCmd
        {
            get { return _openFileCmd ?? (_openFileCmd = new RelayCommand<string>(OpenFile)); }
        }

        private void OpenFile(string obj)
        {
            FileDialog dia = new OpenFileDialog();
            var rlt = dia.ShowDialog();
            if (rlt.HasValue && rlt.Value)
            {
                if (obj.Contains("L"))
                {
                    Op050LConfig.PrinterDemoName = dia.FileName;
                }
                else
                {
                    Op050RConfig.PrinterDemoName = dia.FileName;
                }
            }

        }

        #region 手动打印

        private RelayCommand _qOp050LPrintCmd;

        public RelayCommand QOp050LPrintCmd
        {
            get { return _qOp050LPrintCmd ?? (_qOp050LPrintCmd = new RelayCommand(QOp050LPrint)); }
        }

        private void QOp050LPrint()
        {

        }

        private RelayCommand _qOp050RPrintCmd;

        public RelayCommand QOp050RPrintCmd
        {
            get { return _qOp050RPrintCmd ?? (_qOp050RPrintCmd = new RelayCommand(QOp050RPrint)); }
        }

        private void QOp050RPrint()
        {
            var station = ContainerServer.Container.Resolve<QOP050RStation>();
            //station.TriggerBQPrint_LH
        }


        #endregion


        #region 装箱

        private List<string> _portNames;

        public List<string> PortNames
        {
            get { return _portNames; }
            set => SetProperty(ref _portNames, value);
        }

        private RelayCommand _loadPortsCmd;

        public RelayCommand LoadPortsCmd
        {
            get { return _loadPortsCmd ?? (_loadPortsCmd = new RelayCommand(LoadPorts)); }
        }

        private void LoadPorts()
        {
            PortNames = SerialPort.GetPortNames().ToList();
        }

        #endregion
    }
}
